package manong.controller;

import com.github.pagehelper.PageInfo;
import manong.constant.ServerResponse;
import manong.beans.Classroom;
import manong.beans.ClassroomFree;
import manong.service.IClassroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

//空教室查询 功能 ---》首先controller，
//模糊查询--》类似浏览器的关键字查询
@RestController
@RequestMapping("/classroom/")
public class ClassroomController {

    @Autowired
    private IClassroomService iClassroomService;

    @RequestMapping("add")
    public ServerResponse<String> add(String roomName, Integer roomSpace, Integer roomLayer, String mark) {
        return iClassroomService.add(roomName, roomSpace, roomLayer,mark);
    }

    @RequestMapping("del")
    public ServerResponse<String> del(Integer id) {
        return iClassroomService.del(id);
    }

    @RequestMapping("update")
    public ServerResponse<String> update(Integer id, String roomName, Integer roomSpace, Integer roomLayer,String mark) {
        return iClassroomService.update(id, roomName, roomSpace, roomLayer,mark);
    }

    @RequestMapping("find")
    public ServerResponse<Classroom> find(Integer id) {
        return iClassroomService.find(id);
    }

    @RequestMapping("findAll")
    public ServerResponse<PageInfo<Classroom>> findAll(Integer pageNum, Integer pageSize) {
        return iClassroomService.findAll(pageNum, pageSize);
    }

    @RequestMapping("findByWeek")
    public ServerResponse<PageInfo<ClassroomFree>> findByWeek(Integer pageNum, Integer pageSize, Integer week,String roomName,String day) {
        List<ClassroomFree> byWeek = iClassroomService.findByWeek(week,roomName);
        List<ClassroomFree> collect;
        //把空教室查出来
        // -》跳到业务层
      //*****222222 从业务层返回后，进行判断--》根据day是否为空来判断是否需要过滤---》去重
        if (day!=null&&!"".equals(day)){
             collect= byWeek.stream().filter(s->s.getDay().equals(day)).skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        }else {//filter--》过滤     skip--》去重
            collect= byWeek.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        }
        
        //创建分页对象用于存放数据
        PageInfo<ClassroomFree> pageInfo = new PageInfo<>(collect);
        //设置数据
        pageInfo.setTotal(byWeek.size());
        //返回前端
        return ServerResponse.createBySuccess(pageInfo);
    }

}
/*888888888888888
第二层过滤--》进行一个合法性的校验证--》过滤后放到pageinfo分页对象，创建分页对象用于存放数据--》设置数据--》返回前端

8888888888888888*/






/*
简述
           ！！！！！！！！！！！！！！！！！！！！！！！！！！！！！业务层！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
详细
   走controller
*/
